package ar.edu.itba.tp1.tpmariano;

import java.util.Map;

import ar.edu.itba.tp1.gps.api.GPSState;
import ar.edu.itba.tp1.gps.exception.NotAppliableException;


public abstract class EdgeSwapGPSRule extends SwapGPSRule {
	
	public EdgeSwapGPSRule(int srcX, int srcY, int dstX, int dstY) {
		super(srcX, srcY, dstX, dstY);
	}

	protected abstract Map<Position, Integer> getNumbers();

	@Override
	public GPSState evalRule(GPSState state) throws NotAppliableException {
		Grid swappedGrid = (Grid)super.evalRule(state);
		int numberSrc = getNumbers().get(new Position(srcX,srcY));
		int numberDst = getNumbers().get(new Position(dstX,dstY));
		int quantRotations = numberDst - numberSrc;
		
		if(quantRotations > 0)
			for(int i = 0; i < quantRotations; i++){
				swappedGrid.rotateRight(srcX, srcY);
				swappedGrid.rotateLeft(dstX, dstY);
			}
		else 
			for(int i = 0; i < Math.abs(quantRotations); i++){
				swappedGrid.rotateLeft(srcX, srcY);
				swappedGrid.rotateRight(dstX, dstY);
			}
		return swappedGrid;
	}

}
